<?php
/**
 * This file is part of Hyperf Mall.
 *
 * @link     https://gitee.com/scwlkj/hyperf_mall
 * @contact  hyperf_mall@163.com
 */

namespace App\Repository;

use App\Constants\Adv;
use App\Model\AdvModel;
use App\Utils\Log;
use Hyperf\Di\Annotation\Inject;

class AdvRepository extends BaseRepository
{
    /**
     * @Inject
     * @var AdvModel
     */
    protected $model;


    /**
     * Notes: 返回广告位
     * Author: Bruce.z
     * DateTime: 2022/6/10 13:38
     * @return array
     */
    public function position():array
    {
       return Adv::POSITION_DESC;
    }

    /**
     * Notes: app首页
     * Author: Bruce.z
     * DateTime: 2022/6/13 16:40
     * @return array
     */
    public function indexAdv():array
    {
        $index_adv = [Adv::INDEX_TOP,Adv::INDEX_MIDDLE];

        $condition = [
            'position'=>$index_adv,
            ['start_time','<',date('Y-m-d H:i:s', time())],
            ['end_time','>',date('Y-m-d H:i:s', time())]
        ];

        $res = $this->getRows($condition,['name','icon','back_color','href','sort','position']);

        return collect($res)->groupBy('position')->map(function ($re){
            return collect($re)->sortBy('sort')->toArray();
        })->toArray();
    }
}
